package cn.stylefeng.guns.modular.file.controller;

import cn.stylefeng.guns.modular.car.util.AliOssUtil;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.file.pojo.request.FileRequest;
import cn.stylefeng.guns.modular.file.service.FileService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 控制器
 *
 * @author xbz
 * @date 2023/04/20 15:15
 */
@RestController
@ApiResource(name = "多文件上传")
public class FilesController {

    @Resource
    private FileService fileService;

    /**
     * 添加
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @PostResource(name = "添加", path = "/file/add", requiredPermission = false, requiredLogin = false)
    public ResponseData<File> add(@RequestBody @Validated(FileRequest.add.class) FileRequest fileRequest) {
        fileService.add(fileRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @PostResource(name = "删除", path = "/file/delete", requiredPermission = false, requiredLogin = false)
    public ResponseData<?> delete(@RequestBody @Validated(FileRequest.delete.class) FileRequest fileRequest) {
        fileService.del(fileRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @PostResource(name = "编辑", path = "/file/edit", requiredPermission = false, requiredLogin = false)
    public ResponseData<?> edit(@RequestBody @Validated(FileRequest.edit.class) FileRequest fileRequest) {
        fileService.edit(fileRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看详情
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @GetResource(name = "查看详情", path = "/file/detail", requiredPermission = false, requiredLogin = false)
    public ResponseData<File> detail(@Validated(FileRequest.detail.class) FileRequest fileRequest) {
        return new SuccessResponseData<>(fileService.detail(fileRequest));
    }

    /**
     * 获取列表
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @GetResource(name = "获取列表", path = "/file/list", requiredPermission = false, requiredLogin = false)
    public ResponseData<List<File>> list(FileRequest fileRequest) {
        return new SuccessResponseData<>(fileService.findList(fileRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    @GetResource(name = "分页查询", path = "/file/page", requiredPermission = false, requiredLogin = false)
    public ResponseData<PageResult<File>> page(FileRequest fileRequest) {
        return new SuccessResponseData<>(fileService.findPage(fileRequest));
    }

    /**
     * 文件上传
     */

    @PostResource(name = "文件上传", path = "/files/upload", requiredPermission = false, requiredLogin = false)
    public ResponseData<?> upload(@RequestPart List<MultipartFile> files) throws IOException {

        if (!files.isEmpty()) {
            for (MultipartFile file : files) {
                //1.验证内容是否为空

                //2.获取内容
                byte[] data = file.getBytes();
                String filename = rename(file.getOriginalFilename());
                //3.上传到阿里云的Oss
                AliOssUtil.upload(filename, data);
                //4.返回访问地址
                String url = AliOssUtil.getPath(filename, 1);
                FileRequest file1 = new FileRequest();
                file1.setUrl(url);
                file1.setName(filename);
                file1.setPid(1);
                fileService.add(file1);

            }
            return new SuccessResponseData<>();
        }
        return new ErrorResponseData<>("1001","亲，没有上传文件!");
    }

    /**
     * 对文件名进行长度限制和重命名（保证唯一性）
     */
    public String rename(String name) {
        //1.限制长度
        if (name.length() > 40) {
            name = name.substring(name.length() - 40);
        }
        //2.重命名 保证唯一性
        return UUID.randomUUID().toString() + "_" + name;
    }


}
